import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_model/cartoon_media_info.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_page/find/rank_module/rank_main_logic.dart';
import 'package:tiktokadult/hj_page/find/rank_module/video_rank_cell.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/tools_base/refresh/pull_refresh.dart';
import '../../community_business/widget/community_post_widget.dart';
import 'acg_rank_item.dart';
import 'image_rank_item.dart';
import 'rank_sub_logic.dart';

class RankSubPage extends StatefulWidget {
  final RankType pageType;
  final int sortType; //1 -日榜, 2 -周榜, 3 -月榜,  4 -全部

  const RankSubPage({super.key, required this.pageType, this.sortType = 0});

  @override
  State<RankSubPage> createState() => _RankSubPageState();
}

class _RankSubPageState extends State<RankSubPage> {
  late RankSubPageLogic logic;
  late String tag;

  @override
  void initState() {
    super.initState();
    tag = widget.pageType.toString() + widget.sortType.toString();
    logic = RankSubPageLogic(
      pageType: widget.pageType,
      sortType: widget.sortType,
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<RankSubPageLogic>(
      init: logic,
      tag: tag,
      builder: (_) => pullYsRefresh(
        onInit: (ctr) => _.refreshCtr = ctr,
        onRefresh: (ctr) => _.loadData(),
        onLoading: (ctr) => _.loadMoreData(),
        child: _buildContent(),
      ),
    );
  }

  _buildContent() {
    if (logic.dataList == null) return LoadingCenterWidget();
    if (logic.dataList != null && logic.dataList?.isEmpty == true) return CErrorWidget();
    return ListView.separated(
      padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      itemCount: logic.dataList?.length ?? 0,
      separatorBuilder: (context, index) => 10.sizeBoxH,
      itemBuilder: (context, index) {
        if (logic.pageType == RankType.video) return _buildVideoCell(index);
        if (logic.pageType == RankType.pic) return _buildPicCell(index);
        if (logic.pageType == RankType.post) return _buildPostCell(index);
        if (logic.isACG) return _buildACGCell(index);

        return Container();
      },
    );
  }

  //影视cell
  _buildVideoCell(int index) {
    VideoModel? model = logic.dataList?[index];
    return VideoRankItem(index: index, movie: model);
  }

  //图集cell
  _buildPicCell(int index) {
    VideoModel? model = logic.dataList?[index];
    return ImageRankItem(index, model);
  }

  //帖子
  _buildPostCell(int index) {
    VideoModel model = logic.dataList![index];
    List<VideoModel> list = [];
    for (var model in logic.dataList!) {
      list.add(model);
    }
    return CommunityPostWidget(
      videoModel: model,
      videoModels: list,
      rankIndex: index,
    );
  }

  //小说
  _buildACGCell(int index) {
    CartoonMediaInfo? model = logic.dataList?[index];
    return ACGRankItem(index, model);
  }
}
